var $ = require('../../common/helper.js');
Page({
    data: {
        data: {},
        person_id: '',
        province_id: '',
        city_id: '',
        casy_id: '',
        month: '',
        gjj: '',
        jsApiParameters: null,
        order_sn: ''
    },
    onLoad: function(options) {
        options.month = decodeURIComponent(options.month);
        var _this = this;
        $.post('api.php?app=cart&act=jiesuan', options, {
			success: function (json) {
				_this.setData({
					data: json.data,
					person_id: options.person_id,
					province_id: options.province_id,
					city_id: options.city_id,
					casy_id: options.casy_id,
					month: options.month,
					gjj: options.gjj
				});
			},
			fail:function(){
				wx.navigateBack();
			}
		});
    },
    postForm: function(e) {
        if (this.data.jsApiParameters) {
            this.payment(this.data.jsApiParameters);
            return;
        }
        var _this = this,
            data = e.detail.value;
        if (!data.person_id.length) {
            $.overloadError('请选择参保人');
            return;
        }
        if (!data.province_id.length || !data.city_id.length) {
            $.overloadError('请选择参保地');
            return;
        }
        if (!data.casy_id.length) {
            $.overloadError('请选择参保方案');
            return;
        }
        if (!data.month.length) {
            $.overloadError('请选择参保月份');
            return;
        }
        $.post('api.php?app=cart&act=order_pay', data, function(json) {
            _this.setData({
                jsApiParameters: json.data,
                order_sn: json.order_sn
            });
            _this.payment(json.data);
        });
    },
    payment: function(jsApiParameters) {
        var _this = this;
        $.payment(jsApiParameters, function() {
            wx.navigateTo({
                url: '/pages/cart/complete?order_sn=' + _this.data.order_sn,
            });
        });
    }
})